<?php
  class permit_lib{
      function __construct(){
         $this->CI =& get_instance();
         //$this->check_permit();
      }
      function check_permit(){
         $tbl_pm_dt = $this->CI->db->dbprefix('permission_detail');
         $tbl_pm_admin = $this->CI->db->dbprefix('permission_admin');
         $module = $this->CI->uri->segment(1);
         $func = $this->CI->uri->segment(2);
         $this->CI->db->select($tbl_pm_dt.'.*');
         $this->CI->db->where($tbl_pm_dt.'.permission_name',$module);
         $this->CI->db->where($tbl_pm_dt.'.function_name',$func);
         $check_fn = $this->CI->db->get('permission_detail')->row();
         if($check_fn){        
             $admin_id = $this->CI->session->userdata('admin_id');
             $this->CI->db->select($tbl_pm_dt.'.*, permission_admin.*');
             $this->CI->db->join($tbl_pm_dt,$tbl_pm_dt.'.function_id = '.$tbl_pm_admin.'.function_id');
             $this->CI->db->where($tbl_pm_dt.'.permission_name',$module);
             $this->CI->db->where($tbl_pm_admin.'.admin_id',$admin_id);
             $this->CI->db->where($tbl_pm_dt.'.function_name',$func);
             $check = $this->CI->db->get('permission_admin')->row();
             if(!$check){
                 $this->CI->session->set_flashdata('message','Bạn không có quyền thực thi chức năng trên');
                 //redirect($this->CI->session->userdata('Url'));         
                 redirect(base_url().'admincp');
             }else{
                 $this->write_logs($module,$func);
             }
         }
      }
      
      function write_logs($Modules,$Function){
          $Url = $this->CI->session->userdata('Url');
          $AccountID = $this->CI->session->userdata('admin_id');
          $data = array(
            'admin_id' => $AccountID,
            'modules' => $Modules,
            'function' => $Function,
            'url' => $Url,
            'log_info' =>$this->CI->agent->agent_string(),
            'log_ip' => $this->CI->input->ip_address(),
            'date_log' => time()
          );
          $this->CI->db->insert('logs',$data);
      }
  }
?>
